package cn.itcast.shop.product.adminaction;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.shop.categorysecond.service.CategorySecondService;
import cn.itcast.shop.categorysecond.vo.CategorySecond;
import cn.itcast.shop.product.service.ProductService;
import cn.itcast.shop.product.vo.Product;
import cn.itcast.shop.utils.PageBean;

public class AdminProductAction extends ActionSupport implements ModelDriven<Product> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Product product = new Product();
	@Override
	public Product getModel() {
		return product;
	}
	
	private ProductService productService;
	
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	private CategorySecondService categorySecondService;

	public void setCategorySecondService(CategorySecondService categorySecondService) {
		this.categorySecondService = categorySecondService;
	}
	
	private int page;
	/**
	 * 当前页数
	 * @param page
	 */
	public void setPage(int page) {
		this.page = page;
	}
	
	//文件上传需要的参数：
	private File upload;// 上传的文件 名字与前台JSP页面控件name属性一致
	private String uploadFileName;// 接收文件上传的文件名
	private String uploadContextType;//接收文件上传的文件的MIME的类型

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setUploadContextType(String uploadContextType) {
		this.uploadContextType = uploadContextType;
	}

	private Integer pid;
	
	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public String findAllByPage(){
		
		PageBean<Product> pageBean = productService.findByPage(page);
		ActionContext.getContext().getValueStack().set("pageBean",pageBean);
		return "findAllByPage";
	}
	
	public String addPage(){
		//查询所有的二级分类
		List<CategorySecond> csList= categorySecondService.findAll();
		//通过值栈保存数据
		ActionContext.getContext().getValueStack().set("csList", csList);
		return "addPageSuccess";
	}
	
	public String save() throws IOException{
		//
		product.setPdate(new Date());
		if (upload!=null) {
			// 获得文件上传的磁盘绝对路径
			String realPath = ServletActionContext.getServletContext().getRealPath("/products");
			//创建一个文件：
			File diskFile = new File(realPath+"//"+uploadFileName);
			//文件上传：
			FileUtils.copyFile(upload, diskFile);
			product.setImage("products/"+uploadFileName);
		}
		
		productService.save(product);
		return "saveSuccess";
	}
	
	/**
	 * 删除商品的方法
	 * @return
	 */
	public String delete(){
		//先查询再删除：
		product = productService.findByPid(product.getPid());
		//删除上传的图片：
		String path = product.getImage();
		if (path!=null) {
			try {
				String realPath=ServletActionContext.getServletContext().getRealPath("/"+path);
				File file = new File(realPath);
				file.delete();
			} catch (Exception e) { }
		}
		//删除商品
		productService.delete(product);
		return "deleteSuccess";
	}
	
	public String edit(){
		// 根据商品的ID查询商品
		product = productService.findByPid(product.getPid());
		//查询所有的二级分类的集合
		List<CategorySecond> csList = categorySecondService.findAll();
		
		ActionContext.getContext().getValueStack().set("csList", csList);
		return "editSuccess";
	}
	
	/**
	 * 修改商品的方法：
	 * @return
	 * @throws IOException 
	 */
	public String update() throws IOException{
		product.setPdate(new Date());
		
		if (upload!=null) {
			//将原来上传的商品的图片删除！
			String path = product.getImage();
			File file = new File(ServletActionContext.getServletContext().getRealPath("/"+path));
			file.delete();
			
			// 获得文件上传的磁盘绝对路径
			String realPath = ServletActionContext.getServletContext().getRealPath("/products");
			//创建一个文件：
			File diskFile = new File(realPath+"//"+uploadFileName);
			//文件上传：
			FileUtils.copyFile(upload, diskFile);
			product.setImage("products/"+uploadFileName);
		}
		
		//修改商品的数据到数据库
		productService.update(product);
		return "updateSuccess";
	}
	
	
	
}

































